using System;
using System.Collections.Generic;
using System.Drawing;
namespace Circus.CodeEditor
{
	public class SortRange : Range, ICloneable
	{
		private SortList<Range> ranges;
		private Range range;
		private int index;
		public virtual SortList<Range> Ranges
		{
			get
			{
				return this.ranges;
			}
		}
		public virtual int Index
		{
			get
			{
				return this.index;
			}
		}
		public override Point StartPoint
		{
			get
			{
				return (this.range != null) ? this.range.StartPoint : base.StartPoint;
			}
			set
			{
				if (this.range != null)
				{
					this.range.StartPoint = value;
				}
				else
				{
					base.StartPoint = value;
				}
			}
		}
		public override Point EndPoint
		{
			get
			{
				return (this.range != null) ? this.range.EndPoint : base.EndPoint;
			}
			set
			{
				if (this.range != null)
				{
					this.range.EndPoint = value;
				}
				else
				{
					base.EndPoint = value;
				}
			}
		}
		public Range Range
		{
			get
			{
				return this.range;
			}
		}
		public override void Assign(Range source)
		{
			base.Assign(source);
			this.ranges.Clear();
			if (source is SortRange)
			{
				foreach (Range current in ((SortRange)source).Ranges)
				{
					this.Ranges.Add((Range)current.Clone());
				}
			}
		}
		public override object Clone()
		{
			SortRange sortRange = new SortRange(this.index);
			sortRange.Assign(this);
			return sortRange;
		}
		public SortRange(int index)
		{
			this.index = index;
			this.ranges = new SortList<Range>();
		}
		public SortRange(Range range, int index) : this(index)
		{
			this.range = range;
		}
		public virtual void Clear()
		{
			using (IEnumerator<Range> enumerator = this.ranges.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SortRange sortRange = (SortRange)enumerator.Current;
					sortRange.Clear();
				}
			}
			this.ranges.Clear();
		}
		public virtual bool Contains(Range range)
		{
			return (this.StartPoint.Y < range.StartPoint.Y || (this.StartPoint.Y == range.StartPoint.Y && this.StartPoint.X < range.StartPoint.X)) && (this.EndPoint.Y > range.EndPoint.Y || (this.EndPoint.Y == range.EndPoint.Y && this.EndPoint.X >= range.EndPoint.X));
		}
		public virtual int Add(SortRange range)
		{
			this.ranges.Add(range);
			return this.ranges.Count - 1;
		}
		 
		public virtual bool PositionChanged(int x, int y, int deltaX, int deltaY, RangeList2 list, IComparer<Range> comparer)
		{
			bool result = false;
			Range obj = new Range(x, y, x, y);
			int num;
			if (!this.ranges.FindLast(obj, out num, comparer))
			{
				num--;
			}
			for (int i = Math.Max(num, 0); i < this.ranges.Count; i++)
			{
				SortRange sortRange = (SortRange)this.ranges[i];
				Point startPoint = sortRange.StartPoint;
				if (deltaY == 0 && startPoint.Y > y)
				{
					break;
				}
				if (list.UpdatePosition(sortRange, x, y, deltaX, deltaY))
				{
					result = true;
				}
				if (sortRange.PositionChanged(x, y, deltaX, deltaY, list, comparer))
				{
					result = true;
				}
			}
			return result;
		}
		public virtual bool BlockDeleting(Rectangle rect, RangeList2 list, IComparer<Range> comparer)
		{
			bool result = false;
			Range obj = new Range(rect.Right, rect.Bottom, rect.Right, rect.Bottom);
			int num;
			if (this.ranges.FindLast(obj, out num, comparer))
			{
				num++;
			}
			for (int i = Math.Min(num, this.ranges.Count - 1); i >= 0; i--)
			{
				SortRange sortRange = (SortRange)this.ranges[i];
				Range range = sortRange;
				if (range.EndPoint.Y < rect.Top)
				{
					break;
				}
				if (Circus.CodeEditor.Range.InsideRange(range.StartPoint, rect) && Circus.CodeEditor.Range.InsideRange(new Point(Math.Max(range.EndPoint.X - 1, 0), range.EndPoint.Y), rect))
				{
					if (sortRange.Index < list.Count)
					{
						list.RemoveAt(sortRange.Index);
					}
					this.ranges.RemoveAt(i);
					result = true;
				}
				else
				{
					if (sortRange.BlockDeleting(rect, list, comparer))
					{
						result = true;
					}
				}
			}
			return result;
		}
	}
}
